package com.xs.bookspoststation.BookActivity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.xs.bookspoststation.R;
import com.xs.bookspoststation.adapter.BookAdapter;
import com.xs.bookspoststation.bean.Book;
import com.xs.bookspoststation.db.BookBean;
import com.xs.bookspoststation.db.DBHelperBook;

import java.util.List;

public class BookViewActivity extends AppCompatActivity {
    //定义对象
    TextView sel_bookid;
    private ListView listView2;
    private Button btn_book_back3;
    private List<Book> bookList;
    private BookBean bookBean;
    //定义一个数据库帮助类
    private DBHelperBook dbHelperBook;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_view);
        //绑定控件
        sel_bookid = (TextView) findViewById(R.id.sel_bookid);

        listView2 = (ListView)findViewById(R.id.listView2);
        btn_book_back3 = (Button)findViewById(R.id.btn_book_back3);
        //返回按钮
        btn_book_back3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(BookViewActivity.this, OperateBookActivity.class);
                Toast.makeText(getApplicationContext(), "返回上一页",Toast.LENGTH_LONG).show();
                startActivity(intent);
            }
        });
    }
    /**
     * 启动
     */
    @Override
    protected void onStart() {
        super.onStart();
        getData();
        BookAdapter myAdapter=new BookAdapter(this,R.layout.booklist,bookList);
        listView2.setAdapter(myAdapter);
        listView2.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> arg0,View arg1,int arg2,long arg3) {
                Book book = (Book)arg0.getItemAtPosition(arg2);
                final String bookid = book.getBookid();
                final String bookname = book.getBookname();
                final String booknumber = book.getBooknumber();
                final String bookauthor = book.getBookauthor();
                final String bookshow = book.getBookshow();
                //弹出框选择删除还是修改
                AlertDialog.Builder builder=new AlertDialog.Builder(BookViewActivity.this);
                builder.setTitle("确定以下操作？？？");
                /**
                 * 修改逻辑代码
                 */
                builder.setPositiveButton("修改", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        //启动Activity
                        Intent intent=new Intent(BookViewActivity.this, UpdateBookActivity.class);
                        Bundle bundel=new Bundle();//传入页面数据
                        bundel.putString("bookid", bookid);
                        bundel.putString("bookname", bookname);
                        bundel.putString("booknumber",booknumber);
                        bundel.putString("bookauthor",bookauthor);
                        bundel.putString("bookshow",bookshow);
                        intent.putExtras(bundel);
                        startActivity(intent);
                    }});

                /**
                 * 删除逻辑代码
                 */
                builder.setNegativeButton("删除", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        //创建user对象
                        Book book = new Book(bookid,bookname,booknumber,bookauthor,bookshow);
                        //打开数据库
                        SQLiteDatabase db = dbHelperBook.getWritableDatabase();
                        if(db.isOpen()){
                            //直接根据图书bookid判断这个图书是否存在
                            String bookid = book.getBookid();
                            //调用delete
                            db.delete("bookInfo","bookid=?",new String[]{bookid});
                            //打印日志
                            Log.v("删除图书信息操作","删除图书信息成功！");
                            Toast.makeText(getApplicationContext(), "删除成功", Toast.LENGTH_LONG).show();
                        }else{
                            Toast.makeText(BookViewActivity.this,"数据库打开失败",Toast.LENGTH_LONG).show();
                        }
                        onStart();
                    }});
                builder.show();
            }});
    }

    /**
     * 调用查询方法
     */
    private void getData() {
        dbHelperBook = new DBHelperBook(BookViewActivity.this,"book.db",null,1);
        bookBean = new BookBean(this);
        bookList = bookBean.findAll();
    }
}
